<?php
/**
 * @author Nguyễn Sơn Tùng <sontung.x305@gmail.com> <YM :terminator_nst>
 * @version 1.0
 * @copyright fteam.vn
 * Date: 17/05/2012
 * Time: 22:46
 */
class FUserIdentity extends CUserIdentity {
    /**
     *
     * @var int Id of the User
     */
    private $_id;

    /**
     *
     * @var CActiveRecord current User Model
     */
    private $_model;


    /**
     * This function check the user Authentication
     *
     * @return int
     */
    public function authenticate()
    {
        //$bCrypt = new bCrypt();
        $username=strtolower($this->username);

        if(strpos($username, '@')!==false){
            $user=User::model()->find('LOWER(email)=?',array($username));
        } else {
            $user=User::model()->find('LOWER(username)=?',array($username));
        }

        if($user===null){
            $this->errorCode=self::ERROR_USERNAME_INVALID;

        } elseif(!bCrypt::verify($this->password, $user->password)) {
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        }
        else
        {
            if($user->status!=USER_DEACTIVATE){
                $time = time();
                if($user->status == USER_BANNED && ($user->time_ban > $time || $user->time_ban == NULL)) {
                    $this->errorCode = USER_BANNED;
                } else {
                    $this->_id=$user->id;
                    //If the site allow auto Login, create token to recheck for Cookies
                    if(Yii::app()->user->allowAutoLogin)
                    {
                        $autoLoginToken=sha1(uniqid(mt_rand(),true));
                        $this->setState('autoLoginToken',$autoLoginToken);

                        $db=Yii::app()->db;

                        //delete old keys
                        $command=$db->createCommand('DELETE FROM {{autologin_tokens}} WHERE user_id=:user_id');
                        $command->bindValue(':user_id',$user->id,PDO::PARAM_STR);
                        $command->execute();

                        //set new
                        $command=$db->createCommand('INSERT INTO {{autologin_tokens}}(user_id,token) VALUES(:user_id,:token)');
                        $command->bindValue(':user_id',$user->id,PDO::PARAM_STR);
                        $command->bindValue(':token',$autoLoginToken,PDO::PARAM_STR);
                        $command->execute();

                    }

                    //Start to set the recent_login time for this user
                    $user->recent_login=time();
                    $user->recent_ip = ip();
                    // set status if banned
                    if($user->status == USER_BANNED) {
                        $user->status = USER_ACTIVE;
                        $user->time_ban = NULL;
                    }
                    $user->save();

                    $this->_model=$user;

                    //Set the Error Code to None for Success
                    $this->errorCode=self::ERROR_NONE;
                }

            } else {
                $this->errorCode=USER_DEACTIVATE;
            }
        }

        unset($user);

        return $this->errorCode;
    }

    /**
     * Return the property _id of the class
     * @return bigint
     */
    public function getId()
    {
        return $this->_id;
    }


    /**
     *
     * Return the _model of the class
     * @return CActiveRecord
     */
    public function getModel()
    {
        return $this->_model;
    }
}